// pages/ticket/ticket.js
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog'
var allList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    p: 1,
    active: 0,
    cate: [],
    uid: '',
    invite_code: '',
    emptyTicket: '../image/no-ticket-empty.png',
    wxIcon: '../image/wx.png',
    ticketList: [],
    isLoad: false,
    showUseTicket: false,
    showShare: false,
    number: 1,
    dialogType: 'use',
    productId: 0,
    max: 0,
    sid: 0,
    name: "",
  },
  // 减少数量
  handleReduce() {
    if (this.data.number > 1) {
      this.setData({
        number: this.data.number - 1
      })
    }
  },
  numberInput(e) {
    var val = e.detail.value
    this.setData({
      number: val
    })
  },

  // 增加数量
  handleAdd() {
    if (this.data.number >= this.data.max) {
      return
    }
    this.setData({
      number: this.data.number + 1
    })
  },
  // 切换标签
  onChange(event) {
    var index = `${event.detail.index}`
    var id = this.getId(index);
    this.setData({
      isLoad: false
    })
    console.log(id)
    this.listClear()
    this.loadList(id)
  },
  // 使用水票
  handleUse(e) {
    console.log(e)
    this.setData({
      dialogType: 'use'
    })

    var number = e.currentTarget.dataset.name.number;
    var productId = e.currentTarget.dataset.name.id;
    this.setData({
      productId: productId
    })
    this.setData({
      max: number
    })
    if (number == 1) { // 只有一张，直接使用
      wx.navigateTo({
        url: `/pages/index/index?isuseTicket=useTicket&useTicketNumber=1&productId=${productId}`,
      })
    } else { // 多张，弹出数量选择
      this.setData({
        showUseTicket: true
      })
    }
  },
  // 赠送
  handleSend(e) {
    console.log(e)
    var name = e.currentTarget.dataset.name.title;
    var cate_name = e.currentTarget.dataset.name.product.cate_name;
    this.setData({
      dialogType: 'send',
      showUseTicket: true,
      name: name,
      cate_name: cate_name
    })
    var number = e.currentTarget.dataset.name.number;
    var productId = e.currentTarget.dataset.name.id;
    this.setData({
      productId: productId
    })
    this.setData({
      max: number
    })
  },
  // 弹出框点确定
  handleConfirm() {
    let that = this;
    if (this.data.dialogType == 'use') { // 多张水票使用
      wx.navigateTo({
        url: `/pages/index/index?isuseTicket=useTicket&useTicketNumber=${this.data.number}&productId=${this.data.productId}`,
      })
    } else { // 赠送水票，第一次请求接口
      var number = this.data.number;
      var product_id = this.data.productId;
      
      that.setData({
        number: number,
        product_id: product_id,
      })
      that.setData({
        showUseTicket: false,
        showShare: true
      })
    }
  },
  // 弹出框点取消
  handleCancel() {
    this.setData({
      showUseTicket: false
    })
    this.setData({
      number: 1
    })
  },
  // 关闭底部分享
  closeShare() {
    this.setData({
      showShare: false
    })
  },
  // 清除列表
  listClear() {
    allList = []
    this.setData({
      ticketList: []
    })
  },
  // 获取点击的id
  getId(index) {
    var arr = this.data.cate;
    for (var i in arr) {
      if (i == index) {
        var item = arr[i]
        var id = item['id']
      }
    }
    return id;
  },
  // 初始化用户数据
  initData() {
    let that = this;
    wx.getStorage({
      key: 'uid',
      success: function(res) {
        that.setData({
          uid: res.data
        })
        
      },
    })
    wx.getStorage({
      key: 'invite_code',
      success: function(res) {
        that.setData({
          invite_code: res.data
        })
      },
    })
  },
  loadList(cate) {
    let that = this;
    var uid = "";

    wx.getStorage({
      key: 'uid',
      success: function(res) {
        uid = res.data;
        var data = {
          uid: uid,
          p: that.data.p
        }
        if (cate) {
          data['cate'] = cate
        }
        wx.request({
          url: 'https://api.bshlsqs.com/user_center/myTickets',
          data: data,
          success(res) {
            that.setData({
              isLoad: true
            })
            var list = res.data.data.data.list;
            var pager = res.data.data.pager;

            for (var i = 0; i < list.length; i++) {
              allList.push(list[i])
            }
            if (pager.nowPage == pager.totalPages) { // 只剩下一页
              that.setData({
                ticketList: allList
              })
            } else if (pager.nowPage < pager.totalPages) { // 多页，请求完后统一塞进去
              var nextP = that.data.p + 1;
              that.setData({
                p: nextP
              })
              that.loadList(cate)
            }

          }
        })
      },
    })

  },
  // 获取分类
  getCate() {
    let that = this
    wx.request({
      url: 'https://api.bshlsqs.com/index/cate',
      data: {

      },
      success(res) {
        // that.submitSuccess(res)
        var arr = [{
          cate_name: '全部',
          id: 0
        }]
        for (var i in res.data.data) {
          arr.push(res.data.data[i])
        }
        that.setData({
          cate: arr
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    allList = []
    this.getCate()
    this.initData()
    this.loadList()
  },
  /**
   * 用户点击右上角分享
   */

  
  onShareAppMessage: function(res) {
    var uid = this.data.uid;
    var product_id = this.data.product_id;
    var number = this.data.number;
    var name = this.data.name;
    var cate_name = this.data.cate_name;
    name = encodeURI(name)
    cate_name = encodeURI(cate_name)
    
    return {
      path: `/pages/getTicket/getTicket?oid=${uid}&product_id=${product_id}&number=${number}&name=${name}&cate_name=${cate_name}`
    }
  }
})